package com.thc.servicetest.controller;


import com.thc.servicetest.annotation.AuthToken;
import com.thc.servicetest.kit.ConstantKit;
import com.thc.servicetest.kit.Md5TokenGenerator;
import com.thc.servicetest.model.ResponseTemplate;
import com.thc.servicetest.persistence.beans.User;
import com.thc.servicetest.persistence.beans.UserSys;
import com.thc.servicetest.persistence.beans.UserSysExample;
import com.thc.servicetest.persistence.mapper.UserMapper;
import com.thc.servicetest.persistence.mapper.UserSysMapper;
import com.thc.servicetest.util.RedisUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author cailei.lu
 * @description
 * @date 2018/8/3
 */

@RestController
@Slf4j
@RequestMapping()
public class UserController {

    @Autowired
    Md5TokenGenerator tokenGenerator;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserSysMapper userSysMapper;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiOperation("用户登录接口")
    public ResponseTemplate login(@RequestBody(required = false) JSONObject userInfo) {

        String username = userInfo.getString("username");
        String password = userInfo.getString("password");

//        User curentUser = new User().selectOne(new EntityWrapper<User>()
//                .eq("username",username)
//                .eq("password",password)
//                .eq("del_flag",0));

        UserSysExample userSysExample = new UserSysExample();
        userSysExample.or().andPasswordEqualTo(password).andUsernameEqualTo(username);
        UserSys currentUser = userSysMapper.selectByExample(userSysExample).get(0);


        JSONObject result = new JSONObject();
        if (currentUser != null) {

            Jedis jedis = new Jedis("localhost", 6379);
            String token = tokenGenerator.generate(username, password);
            jedis.set(username, token);
            jedis.expire(username, ConstantKit.TOKEN_EXPIRE_TIME);
            jedis.set(token, username);
            jedis.expire(token, ConstantKit.TOKEN_EXPIRE_TIME);
            Long currentTime = System.currentTimeMillis();
            jedis.set(token + username, currentTime.toString());

            //用完关闭
            jedis.close();

            result.put("status", "登录成功");
            result.put("token", token);
        } else {
            result.put("status", "登录失败");
        }
        ResponseTemplate responseTemplate=new ResponseTemplate();
        responseTemplate.setCode(200);
        responseTemplate.setData(result);
        responseTemplate.setMessage("登录成功");
        return responseTemplate;

    }

    @ApiOperation("测试token接口")
    @RequestMapping(value = "test", method = RequestMethod.POST)
    @AuthToken
    public ResponseTemplate test(HttpServletRequest httpServletRequest) {

        String username = String.valueOf(redisUtil.get(httpServletRequest.getHeader("token")));

        UserSysExample userSysExample = new UserSysExample();
        userSysExample.or().andUsernameEqualTo(username);
        List<UserSys> user = userSysMapper.selectByExample(userSysExample);

        ResponseTemplate responseTemplate=new ResponseTemplate();
        responseTemplate.setCode(200);
        responseTemplate.setData(user);
        responseTemplate.setMessage("success");
        return responseTemplate;

    }
}
